package leetcode.day;

import java.util.Arrays;
import java.util.HashSet;

// 26. 删除有序数组中的重复项
public class RemoveDuplicates {

    public static void main(String[] args) {

        int[] data = {1, 1, 2};
        System.out.println(removeDuplicates(data));
    }

    // O(n) 暴力循环
    public static int removeDuplicates(int[] nums) {
        int index = 0;
        for (int i = 0; i < nums.length; i++) {
            if (i == nums.length - 1 || nums[i] != nums[i + 1]) {
                nums[index++] = nums[i];
            }
        }
        System.out.println(Arrays.toString(nums));
        return index;
    }

    // 双指针
    public static int removeDuplicates2(int[] nums) {
        if (nums == null ||nums.length == 0  ) {
            return 0;
        }
        int p = 0;
        int q = 1;
        while (q<nums.length){
            if (nums[p]!=nums[q]){
                nums[p+1]=nums[q];
                p++;

            }
            q++;
        }
        return p+1;
    }
}
